package com.gibs.peixunapi.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import javax.validation.Valid;

import org.springframework.transaction.annotation.Transactional;
import com.gibs.peixunapi.result.PageData;
import com.gibs.peixunapi.enums.ResultEnum;
import com.gibs.peixunapi.result.Result;
import com.gibs.peixunapi.utils.ResultUtil;
import com.gibs.peixunapi.dao.AnswerSheetDao;
import com.gibs.peixunapi.service.AnswerSheetService;
import com.gibs.peixunapi.model.AnswerSheet;

@Service
@Slf4j
public class AnswerSheetServiceImpl implements AnswerSheetService{

	@Autowired
	private AnswerSheetDao answerSheetDao;

	@Transactional
@Override
	public Result create(@Valid AnswerSheet object) {
		try {
		
			answerSheetDao.save(object);
			return ResultUtil.success(ResultEnum.CREATE_SUCCESS);
		} catch (Exception e) {
			log.error(e.getMessage());
			return ResultUtil.error(ResultEnum.SERVER_ERROR);
		}
	}

	@Transactional
@Override
	public Result save(@Valid AnswerSheet object) {
		try {
			
//			AnswerSheet answerSheet = answerSheetDao.findOne(object.getId());
//			ConverterUtil.copyNotNullProperties(object,answerSheet);
//			answerSheetDao.save(answerSheet);
			return ResultUtil.success(ResultEnum.UPDATE_SUCCESS);
		} catch (Exception e) {
			log.error(e.getMessage());
			return ResultUtil.error(ResultEnum.SERVER_ERROR);
		}
	}

@Override
	public Result delete(Integer id) {
		try {
//			String[] ids = id.split(",");
//			for (String str : ids) {
//				answerSheetDao.delete(Integer.valueOf(str));
//			}
			return ResultUtil.success(ResultEnum.DELETE_SUCCESS);
		} catch (Exception e) {
			log.error(e.getMessage());
			return ResultUtil.error(ResultEnum.SERVER_ERROR);
		}
	}


@Override
	public Result  get(Integer id) {
		try {
			HashMap<String, Object> data = new HashMap<String, Object>();
			AnswerSheet object = answerSheetDao.getOne(id);
//			if (object == null) {
//				Result(false, Status.FAILD, "查找失败", null);
//			}
//			HashMap<String,Object> map = new HashMap();
//			map.put("id", object.getId());
//			HashMap<String,Object> studentAndExam_id = new HashMap<String,Object>();
//			studentAndExam_id.put("name", object.getStudentAndExam_id()==null?"":object.getStudentAndExam_id().getName());
//			studentAndExam_id.put("id", object.getStudentAndExam_id()==null?"":object.getStudentAndExam_id().getId());
//			map.put("studentAndExam_id", studentAndExam_id);
//			HashMap<String,Object> testPaperSubjectId = new HashMap<String,Object>();
//			testPaperSubjectId.put("name", object.getTestPaperSubjectId()==null?"":object.getTestPaperSubjectId().getName());
//			testPaperSubjectId.put("id", object.getTestPaperSubjectId()==null?"":object.getTestPaperSubjectId().getId());
//			map.put("testPaperSubjectId", testPaperSubjectId);
//			map.put("answer", object.getAnswer()==null?"":object.getAnswer());
//			map.put("score", object.getScore()==null?"":object.getScore());
//			HashMap<String,Object> markMan_id = new HashMap<String,Object>();
//			markMan_id.put("name", object.getMarkMan_id()==null?"":object.getMarkMan_id().getName());
//			markMan_id.put("id", object.getMarkMan_id()==null?"":object.getMarkMan_id().getId());
//			map.put("markMan_id", markMan_id);
//			HashMap<String,Object> markTime = new HashMap<String,Object>();
//			markTime.put("name", object.getMarkTime()==null?"":object.getMarkTime().getName());
//			markTime.put("id", object.getMarkTime()==null?"":object.getMarkTime().getId());
//			map.put("markTime", markTime);
//			data.put("object",map);

			return ResultUtil.success(ResultEnum.FIND_SUCCESS);
		} catch (Exception e) {
			log.error(e.getMessage());
			return ResultUtil.error(ResultEnum.SERVER_ERROR);
		}
	}

	@Override
	public Result  getList(String key) {
		try {
			List<AnswerSheet> list = new ArrayList<>();
			if (!StringUtils.isEmpty(key)) {
				list = answerSheetDao.findLike("%" + key + "%");
			} else {
				list = answerSheetDao.findAll();
			}
//			if (list.size() == 0) {
//				PageResult(true, Status.OK, "无数据", new ArrayList(), 0L,0,0);
//			}
//			List<Object> data = new ArrayList<>();
//			for(AnswerSheet object : list){
//				HashMap<String,Object> map = new HashMap();
//				map.put("id", object.getId());
//				HashMap<String,Object> studentAndExam_id = new HashMap<String,Object>();
//				studentAndExam_id.put("name", object.getStudentAndExam_id()==null?"":object.getStudentAndExam_id().getName());
//				studentAndExam_id.put("id", object.getStudentAndExam_id()==null?"":object.getStudentAndExam_id().getId());
//				map.put("studentAndExam_id", studentAndExam_id);
//				HashMap<String,Object> testPaperSubjectId = new HashMap<String,Object>();
//				testPaperSubjectId.put("name", object.getTestPaperSubjectId()==null?"":object.getTestPaperSubjectId().getName());
//				testPaperSubjectId.put("id", object.getTestPaperSubjectId()==null?"":object.getTestPaperSubjectId().getId());
//				map.put("testPaperSubjectId", testPaperSubjectId);
//				map.put("answer", object.getAnswer()==null?"":object.getAnswer());
//				map.put("score", object.getScore()==null?"":object.getScore());
//				HashMap<String,Object> markMan_id = new HashMap<String,Object>();
//				markMan_id.put("name", object.getMarkMan_id()==null?"":object.getMarkMan_id().getName());
//				markMan_id.put("id", object.getMarkMan_id()==null?"":object.getMarkMan_id().getId());
//				map.put("markMan_id", markMan_id);
//				HashMap<String,Object> markTime = new HashMap<String,Object>();
//				markTime.put("name", object.getMarkTime()==null?"":object.getMarkTime().getName());
//				markTime.put("id", object.getMarkTime()==null?"":object.getMarkTime().getId());
//				map.put("markTime", markTime);
//				data.add(map);
//			}
			return ResultUtil.success(ResultEnum.FIND_SUCCESS);
		} catch (Exception e) {
			log.error(e.getMessage());
			return ResultUtil.error(ResultEnum.SERVER_ERROR);
		}
	}

@Override
	public Result getListPage(String key, Integer current, Integer limit) {
		try {
			List<AnswerSheet> list = new ArrayList<>();
			limit = limit == null ? 10 : limit;
			current = current == null ? 1 : current;
			int offset = (current - 1) * limit;
			Long total = null;
//			if (!StringUtils.isEmpty(key)) {
//				list = answerSheetDao.findPageLike("%" + key + "%", offset, limit);
//				total = answerSheetDao.countLike("%" + key + "%");
//			} else {
//				list = answerSheetDao.findPage(offset, limit);
//				total = answerSheetDao.count();
//			}
//			if (total == 0) {
//				PageResult(true, Status.OK, "无数据", new ArrayList(), 0L,0,0);
//			}
//			List<Object> data = new ArrayList<>();
//			for(AnswerSheet object : list){
//				HashMap<String,Object> map = new HashMap();
//				map.put("id", object.getId());
//				HashMap<String,Object> studentAndExam_id = new HashMap<String,Object>();
//				studentAndExam_id.put("name", object.getStudentAndExam_id()==null?"":object.getStudentAndExam_id().getName());
//				studentAndExam_id.put("id", object.getStudentAndExam_id()==null?"":object.getStudentAndExam_id().getId());
//				map.put("studentAndExam_id", studentAndExam_id);
//				HashMap<String,Object> testPaperSubjectId = new HashMap<String,Object>();
//				testPaperSubjectId.put("name", object.getTestPaperSubjectId()==null?"":object.getTestPaperSubjectId().getName());
//				testPaperSubjectId.put("id", object.getTestPaperSubjectId()==null?"":object.getTestPaperSubjectId().getId());
//				map.put("testPaperSubjectId", testPaperSubjectId);
//				map.put("answer", object.getAnswer()==null?"":object.getAnswer());
//				map.put("score", object.getScore()==null?"":object.getScore());
//				HashMap<String,Object> markMan_id = new HashMap<String,Object>();
//				markMan_id.put("name", object.getMarkMan_id()==null?"":object.getMarkMan_id().getName());
//				markMan_id.put("id", object.getMarkMan_id()==null?"":object.getMarkMan_id().getId());
//				map.put("markMan_id", markMan_id);
//				HashMap<String,Object> markTime = new HashMap<String,Object>();
//				markTime.put("name", object.getMarkTime()==null?"":object.getMarkTime().getName());
//				markTime.put("id", object.getMarkTime()==null?"":object.getMarkTime().getId());
//				map.put("markTime", markTime);
//				data.add(map);
//			}
//			PageData pageData = new PageData(total, limit, current);
			return ResultUtil.successPage(ResultEnum.SUCCESS);
		} catch (Exception e) {
			log.error(e.getMessage());
			return ResultUtil.error(ResultEnum.SERVER_ERROR);
		}
	}

	

@Override
	public Result getOptions(String key) {
		try {
			List<AnswerSheet> list = new ArrayList<>();
			if (!StringUtils.isEmpty(key)) {
				list = answerSheetDao.findLike("%" + key + "%");
			} else {
				list = answerSheetDao.findAll();
			}
			List<Object> data = new ArrayList<>();
			for (AnswerSheet object : list) {
				HashMap<String, Object> map = new HashMap();
				map.put("value", object.getId());
//				map.put("name", object.getStudentAndExam_id());
				data.add(map);
			}
			return ResultUtil.success(ResultEnum.SUCCESS);
		} catch (Exception e) {
			log.error(e.getMessage());
			return ResultUtil.error(ResultEnum.SERVER_ERROR);
		}
	}


}

